package com.bw.lib_common.utils


import android.content.Context
import android.graphics.Paint
import android.text.Editable
import android.view.View
import android.widget.*
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.bumptech.glide.request.RequestOptions
import com.bw.lib_common.livedata_eventbus.Events
import com.bw.lib_common.livedata_eventbus.LiveDataEventBus
import com.bw.lib_storage.UserMMKV
/**
 * @author ykj
 * @date 2025-11-04 20:55
 * @description
 */

/**
 * 双击间隔时间
 */
const val INTERVAL = 400

/**
 * 防重复点击
 */
fun View.setSingClick(function: (View) -> Unit): View {
    var temTime: Long = 0
    this.setOnClickListener {
        if (System.currentTimeMillis() - temTime > INTERVAL) {
            temTime = System.currentTimeMillis()
            function(it)
        }
    }
    return this
}

/**
 * 防重复点击,需要登录
 */
fun View.setLoginSingClick(function: (View) -> Unit): View {
    var temTime: Long = 0
    this.setOnClickListener {
        //判断是否需要登录
        if (UserMMKV.token.isEmpty()){
            Toast.makeText(this.context,"请先登录",Toast.LENGTH_LONG).show()
            LiveDataEventBus.postEvent(Events.EVENT_GO_TO_LOGIN, true)
            return@setOnClickListener
        }
        if (System.currentTimeMillis() - temTime > INTERVAL) {
            temTime = System.currentTimeMillis()
            function(it)
        }
    }
    return this
}

/**
 * 防重复点击,可设置点击间隔
 */
fun View.setSingClick(interval: Int, function: (View) -> Unit): View {
    var temTime: Long = 0
    this.setOnClickListener {
        if (System.currentTimeMillis() - temTime > interval) {
            temTime = System.currentTimeMillis()
            function(it)
        }
    }
    return this
}

/**
 * 防重复点击，多个控件共用一个监听使用
 */
fun setSingClick(vararg views: View, function: (View) -> Unit) {
    var temTime: Long = 0
    val clickLis = object : View.OnClickListener {
        override fun onClick(v: View) {
            if (System.currentTimeMillis() - temTime > INTERVAL) {
                temTime = System.currentTimeMillis()
                function(v)
            }
        }
    }
    for (view in views) {
        view.setOnClickListener(clickLis)
    }
}